package fichier;
import java.io.File;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;


public class Image {
	private int ID;
	private File imageFile;
	private static final String table = "image";
	public static final ArrayList < Image > imageList = new ArrayList < Image >();
	
	
	
	
	public Image(int iD) {
		super();
		ID = iD;
	}
	public Image(File imageFile) {
		super();
		
		this.imageFile = imageFile;
		this.ajouter();
		Image.imageList.add(this);
	}
	public Image(int iD, File imageFile) {
		super();
		ID = iD;
		this.imageFile = imageFile;
		Image.imageList.add(this);
	}
	private void ajouter(){
		SelectData.REQUETE  = "INSERT INTO "+ table +" VALUES (null, '"+ SQLadaptString.adapt(this.imageFile.toString()) +"')";
		System.out.println(SelectData.REQUETE);
		SelectData.startUpdate();
		SelectData.REQUETE = "SELECT MAX(id_image) as 'test' FROM "+ table;
		ResultSet result = SelectData.startSelect();
		String s;
		try {
			while(result.next()){
				s = result.getString("test");
				int lastID = Integer.parseInt( s );
				this.setID(lastID);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void supprimer(){
		SelectData.REQUETE = "DELETE FROM "+ table +" WHERE id_image ="+this.getID();
		System.out.println(SelectData.REQUETE);
		SelectData.startUpdate();
	}
	// GETTERS
	
	public int getID() {
		return ID;
	}
	public File getImageFile() {
		return imageFile;
	}
	

	// SETTERS
	
	public void setID(int iD) {
		ID = iD;
	}
	public void setImageFile(File imageFile) {
		this.imageFile = imageFile;
	}
	
}
